/**
*
* */
Rsd.define('Rsd.Faith.reports.AnalysisPage', {
    extend: 'Rsd.container.Page',
    xtype:'analysis-page',
    requires: [
        'Rsd.control.Chart',
        'Rsd.control.Scaler',
        'Rsd.control.Label',
        'Rsd.control.Image',
        'Rsd.control.ListView'],
    height:1500,
    style:{overFlow:'auto'},
    cls: 'x-dashboard',
    overFlow:'auto',
    items:[
        {
            width: '100%',
            height: 175,
            margin: '20 0 10 0',
            layout: 'hbox',
            items: [
                {
                    xtype: 'chart',
                    height: 150,
                    flex:1,
                    margin: '0 10 0 10',
                    cls: 'x-dashboard-box',
                    label: {
                        xtype:'container',
                        width: 150,
                        height: '100%',
                        position:'right',
                        layout:'vbox',
                        items:[
                            {
                                xtype:'scaler',
                                flex:1,
                                cls:null,
                                margin: '10 10 10 10',
                                style:{backgroundColor:'#fff',textAlign:'right'},
                                label:{content:'小区总数',  position:'top',space:0,style:{lineHeight:'40px'}},
                                color:'rgba(45, 26, 206, 0.9)',
                                unit:'',
                                zoom:200,
                                value:'350.00'
                            },
                            {
                                xtype:'scaler',
                                flex:1,
                                cls:null,
                                margin: '10 10 10 10',
                                style:{backgroundColor:'#fff',textAlign:'right'},
                                label:{content:'使用小区',  position:'top',space:0,style:{lineHeight:'30px'}},
                                color:'rgba(206, 64, 97, 0.9)',
                                unit:'',
                                zoom:150,
                                timer:58,
                                value:'58.00'
                            }
                        ]

                    }
                },
                {
                    xtype: 'chart',
                    height: 150,
                    flex:1,
                    margin: '0 10 0 10',
                    cls: 'x-dashboard-box',
                    label: {
                        xtype:'scaler',
                        width: 180,
                        height: '100%',
                        position:'right',
                        cls:null,
                        margin: '10 10 10 10',
                        style:{backgroundColor:'#fff'},
                        label:{content:'房屋数量',  position:'top',space:0,style:{lineHeight:'40px'}},
                        color:'rgba(45, 26, 206, 0.9)',
                        unit:'户',
                        zoom:200,
                        value:'7000.00'
                    }
                },
                {
                    xtype: 'chart',
                    height: 150,
                    flex:1,
                    margin: '0 10 0 10',
                    cls: 'x-dashboard-box',
                    label: {
                        xtype:'scaler',
                        width: 200,
                        height: '100%',
                        position:'right',
                        cls:null,
                        margin: '10 10 10 10',
                        style:{backgroundColor:'#fff'},
                        label:{content:'建筑面积',  position:'top',space:0,style:{lineHeight:'40px'}},
                        color:'rgba(45, 26, 206, 0.9)',
                        unit:'平方',
                        zoom:200,
                        value:'516000.00'
                    }
                }
            ]
        },
        {
            cls:'x-dashboard-box',
            width:'100%',
            height:350,
            cls:'x-dashboard-box',
            margin:'20 10 20 10',
            items:[

                {
                    xtype:'list-view',
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    //visible:false,
                    itemStyle:{width:'100%'},
                    label:{
                        position:'left',
                        height: '100%',
                        width:100,
                        xtype:'image',
                        src:'https://img.alicdn.com/tfs/TB1iFKccamWBuNjy1XaXXXCbXXa-140-140.png',space:40},
                    dataSource:[
                        {text:'维修申请(元)',style:{border:'none'}},
                        {text:'982.00',style:{height:'40px',lineHeight:'40px',fontSize:'150%' }},
                        {text:'去年：680.00',style:{border:'none'}}
                    ]
                },
                {
                    xtype:'list-view',
                    label:null,
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    //visible:false,
                    itemStyle:{width:'100%'},
                    dataSource:
                        [
                            '维修申请数',
                            {text:80,style:{height:'40px',lineHeight:'40px',fontSize:'150%' }},
                            '去年：60'
                        ]
                },

                {
                    xtype:'list-view',
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    //visible:false,
                    itemStyle:{width:'100%'},
                    label:{
                        position:'left',
                        height: '100%',
                        width:100,
                        xtype:'image',
                        src:'https://img.alicdn.com/tfs/TB1iFKccamWBuNjy1XaXXXCbXXa-140-140.png',
                        space:40
                    },
                    dataSource:[
                        '维修申请拨款(元)',
                        '982.00',
                        '昨日：680.00'
                    ]
                },
                {
                    xtype:'list-view',
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    label:null,
                    //visible:false,
                    itemStyle:{width:'100%'},
                    dataSource:[
                        '维修申请拨款笔数',
                        '80',
                        '去年：60'
                    ]
                },

                {
                    height:10,
                    width:'100%',
                    style:{backgroundColor:'rgba(240, 248, 255, 0.7)'}
                },

                {
                    xtype: 'list-view',
                    margin: '20 0 20 20',
                    height: 150,
                    width: '25%',
                    //visible:false,
                    itemStyle:{width:'100%'},
                    label: {
                        position:'left',
                        height: '100%',
                        width:100,
                        xtype:'image',
                        src: 'https://img.alicdn.com/tfs/TB1iFKccamWBuNjy1XaXXXCbXXa-140-140.png',
                        space: 40
                    },
                    dataSource: [
                        '维修资金缴存(元)',
                        '982.00',
                        '去年：680.00'
                    ]
                },
                {
                    xtype:'list-view',
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    label:null,
                    //visible:false,
                    itemStyle:{width:'100%'},
                    dataSource:['维修资金缴存笔数','80','去年：60']
                },
                {
                    xtype:'list-view',
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    //visible:false,
                    itemStyle:{width:'100%'},
                    label:{
                        position:'left',
                        height: '100%',
                        width:100,
                        xtype:'image',
                        src:'https://img.alicdn.com/tfs/TB1iFKccamWBuNjy1XaXXXCbXXa-140-140.png',
                        space:40
                    },
                    dataSource:['增值收益(元)','982.00','去年：680.00']
                },
                {
                    xtype:'list-view',
                    margin:'20 0 20 20',
                    height:150,
                    width:'25%',
                    label:null,
                    //visible:false,
                    itemStyle:{width:'100%'},
                    dataSource:['投资笔数','80','去年：60']
                }
            ]
        },
        {
            xtype:'chart',
            cls:'x-dashboard-box',
            margin:'5 5 10 10',
            width:'50%',
            height:500,

            label:'增值投资分布'
        },
        {
            xtype:'chart',
            margin:'5 10 10 5',
            cls:'x-dashboard-box',
            width:'50%',
            height:500,
            label:'资金分布'
        },
        {
            xtype:'chart',
            cls:'x-dashboard-box',
            margin:'5 5 10 10',
            width:'50%',
            height:500,

            label:'交存TOP10'
        },
        {
            xtype:'chart',
            margin:'5 10 10 5',
            cls:'x-dashboard-box',
            width:'50%',
            height:500,
            label:'使用TOP10'
        }

    ],
    tabTitle:'概况',
    /*
    * */
    constructor: function constructor (config) {
        config = config || {};
        this.apply(config);
    },
    /*
    * */
    load:function load() {

        var me  = this;
        this.callParent();

        // this.items[0].showLoading(Rsd.lang('container.chart.loading'));
        // this.items[1].showLoading(Rsd.lang('container.chart.loading'));
        // this.items[2].showLoading(Rsd.lang('container.chart.loading'));
        // this.items[3].showLoading(Rsd.lang('container.chart.loading'));

        setTimeout(function () {
            me.initialChart();
        },1000);

        return this;
    },
    /*
    *
    * */
    initialChart:function initialChart() {

        var me = this;

        this.items[1].items[0].loadData().show('fadeIn',500);
        this.items[1].items[1].loadData().show('fadeIn',500);
        this.items[1].items[2].loadData().show('fadeIn',500);
        this.items[1].items[3].loadData().show('fadeIn',500);
        //----------
        this.items[1].items[5].loadData().show('fadeIn',1000);
        this.items[1].items[6].loadData().show('fadeIn',1000);

        this.items[1].items[7].loadData().show('fadeIn',1000);
        this.items[1].items[8].loadData().show('fadeIn',1000);


        var option_1 = {
            title : {
                text: '资金缴存小区排名',
                subtext: 'DEMO'
            },
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:['2011年', '2012年']
            },
            toolbox: {
                show : true,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            xAxis : [
                {
                    type : 'value',
                    boundaryGap : [0, 0.01]
                }
            ],
            yAxis : [
                {
                    type : 'category',
                    data : ['幸福花园','阳光小区','金水湾','迪雅苑','米兰香榭','南园新村']
                }
            ],
            series : [
                {
                    name:'2011年',
                    type:'bar',
                    data:[18203, 23489, 29034, 104970, 131744, 630230]
                },
                {
                    name:'2012年',
                    type:'bar',
                    data:[19325, 23438, 31000, 121594, 134141, 681807]
                }
            ]
        };
        var option_2 = {
            title : {
                text: '资金使用小区排名',
                subtext: 'DEMO'
            },
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:['2011年', '2012年']
            },
            toolbox: {
                show : true,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            xAxis : [
                {
                    type : 'value',
                    boundaryGap : [0, 0.01]
                }
            ],
            yAxis : [
                {
                    type : 'category',
                    data : ['江南花苑','惠众花苑','新发花苑','汇锦新城','鸣翠山庄','金冠华庭']
                }
            ],
            series : [
                {
                    name:'2011年',
                    type:'bar',
                    data:[2103, 3489, 5034, 9070, 13144, 30230]
                },
                {
                    name:'2012年',
                    type:'bar',
                    data:[3125, 3438, 6000, 11594, 14141, 41807]
                }
            ]
        };
        this.items[4].loadData(option_1);
        this.items[5].loadData(option_2);

        Rsd.app.getService('report.global').requestJson(null,function (data) {
            if($empty(data))
            {
                return;
            }
            var _data = Rsd.toJson(data.data);

            var _m = {};
            var _x = [];

            console.log('统计数据，演示时不显示',_data);
            return;
            //===========
            var _order_count = {returnStatus:'999'};
            try {

                _order_count = Rsd.toJson(_data['ordercount']);
            }
            catch (error)
            {
                Rsd.error(_data['ordercount']);
            }


            if(_order_count.returnStatus == '000') {
                var list = _order_count.returnData.dataList;

                for(var i in list)
                {
                    if(_m.hasOwnProperty(list[i]['date']))
                    {
                        _m[list[i]['date']].y12 = list[i]['orderNums'];
                    }
                    else
                    {
                        _m[list[i]['date']] ={date:list[i]['date'],y12:list[i]['orderNums']};
                        _x.push(list[i]['date']);
                    }


                }

            }
            //=============
            var _order = {returnStatus:'999'};

            try {

                _order = Rsd.toJson(_data['order']);
            }
            catch (error)
            {
                Rsd.error(_data['order']);
            }

            if(_order.returnStatus == '000') {
                var list = _order.returnData.dataList;


                for(var i in list)
                {
                    if(_m.hasOwnProperty(list[i]['date']))
                    {
                        _m[list[i]['date']].y11 = list[i]['orderAmount'];
                    }
                    else
                    {
                        _m[list[i]['date']] ={date:list[i]['date'],y11:list[i]['orderAmount']};
                        _x.push(list[i]['date']);
                    }


                }

            }

            //==============
            var _orderBill = {returnStatus:'999'};

            try {

                _orderBill = Rsd.toJson(_data['orderbill']);
            }
            catch (error)
            {
                Rsd.error(_data['orderbill']);
            }

            if(_orderBill.returnStatus == '000') {
                var list = _orderBill.returnData.billAmountList;

                for(var i in list)
                {
                    if(_m.hasOwnProperty(list[i]['time']))
                    {
                        _m[list[i]['time']].y2 = list[i]['billAmount'];
                    }
                    else
                    {
                        _m[list[i]['time']] ={date:list[i]['time'],y2:list[i]['billAmount']};
                        _x.push(list[i]['time']);
                    }
                }

            }


            //==========

            var _shop = {returnStatus:'999'};

            try {

                _shop = Rsd.toJson(_data['shop']);
            }
            catch (error)
            {
                Rsd.error(_data['shop']);
            }


            if(_shop.returnStatus == '000') {

                var list = _shop.returnData;

                var _count = list.length;
                for(var i in list)
                {
                    //var index = i;//parseInt(_count-i-1);倒序算法
                    if(_m.hasOwnProperty(list[i]['date']))
                    {
                        _m[list[i]['date']].y31 = list[i]['num'];
                        _m[list[i]['date']].y32 = list[i]['total'];
                    }
                    else
                    {
                        _m[list[i]['date']] ={date:list[i]['date'],y31:list[i]['num'],y32:list[i]['total']};
                        _x.push(list[i]['date']);
                    }

                }

            }

            //==========

            var _member = {returnStatus:'999'};

            try {

                _member = Rsd.toJson(_data['member']);
            }
            catch (error)
            {
                Rsd.error(_data['member']);
            }


            if(_member.returnStatus == '000') {

                var list = _member.returnData;

                var _count = list.length;
                for(var i in list)
                {
                    //var index = i;//parseInt(_count-i-1);倒序算法
                    if(_m.hasOwnProperty(list[i]['date']))
                    {
                        _m[list[i]['date']].y41 = list[i]['num'];
                        _m[list[i]['date']].y42 = list[i]['total'];
                    }
                    else
                    {
                        _m[list[i]['date']] ={date:list[i]['date'],y41:list[i]['num'],y42:list[i]['total']};
                        _x.push(list[i]['date']);
                    }

                }

            }



            //======


            _x.sort();

            var _y11=[],_y12=[],_y2=[],_y31=[],_y32=[],_y41=[],_y42=[];

            for (var  i in _x)
            {
                var _t = _m[_x[i]];
                _y11.push(_t.y11||0);
                _y12.push(_t.y12||0);
                _y2.push(_t.y2||0);
                _y31.push(_t.y31||0);
                _y32.push(_t.y32||0);
                _y41.push(_t.y41||0);
                _y42.push(_t.y42||0);
            }

            var _chartOp1 = {
                tooltip: {
                    trigger: 'axis'
                },
                /* toolbox: {
                 show : true,
                 feature : {
                 mark : {show: true},
                 dataView : {show: true, readOnly: false},
                 magicType: {show: true, type: ['line', 'bar']},
                 restore : {show: true},
                 saveAsImage : {show: true}
                 }
                 },*/
                calculable : true,
                legend: {
                    data: ['订单金额','订单数量']
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: _x
                    }
                ],
                yAxis: [
                    {
                        type : 'value',
                        name : '金额',
                        axisLabel : {
                            formatter: '{value} 元'
                        }
                    },
                    {
                        type : 'value',
                        name : '数量',
                        axisLabel : {
                            formatter: '{value}笔'
                        }
                    }
                ],
                series: [
                    {
                        name: '订单金额',
                        type: 'line',
                        data: _y11
                    },{
                        name: '订单数量',
                        itemStyle: {
                            normal: {color: 'green'}
                        },
                        type: 'bar',
                        yAxisIndex: 1,
                        data: _y12
                    }
                ]
            };

            me.items[0].load(_chartOp1);

            var _chartOp2 = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['结算金额(单位：元)']
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: _x
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: '结算金额(单位：元)',
                        type: 'line',
                        data: _y2
                    }
                ]
            };

            me.items[1].load(_chartOp2);

            var _chartOp3 = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['新增商家','商家总数']
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: _x
                    }
                ],
                yAxis: [

                    {
                        type : 'value',
                        name : '新增',
                        axisLabel : {
                            formatter: '{value} 家'
                        }
                    },
                    {
                        type : 'value',
                        name : '总数',
                        axisLabel : {
                            formatter: '{value}家'
                        }
                    }

                ],
                series: [
                    {
                        name: '新增商家',
                        type: 'bar',
                        itemStyle: {
                            normal: {color: 'grey'}
                        },
                        data: _y31
                    },
                    {
                        name: '商家总数',
                        type: 'line',
                        itemStyle: {
                            normal: {color: 'blue'}
                        },
                        yAxisIndex: 1,
                        data: _y32
                    }
                ]
            };

            me.items[2].load(_chartOp3);

            var _chartOp4 = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['新增会员','会员总数']
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: _x
                    }
                ],
                yAxis: [
                    {
                        type : 'value',
                        name : '新增',
                        axisLabel : {
                            formatter: '{value} 人'
                        }
                    },
                    {
                        type : 'value',
                        name : '总数',
                        axisLabel : {
                            formatter: '{value}人'
                        }
                    }

                ],
                series: [
                    {
                        name: '新增会员',
                        type: 'bar',
                        itemStyle: {
                            normal: {color: 'rgba(0,0,0,0.65)'}
                        },
                        data: _y41
                    },
                    {
                        name: '会员总数',
                        type: 'line',
                        yAxisIndex: 1,
                        color:'green',
                        data: _y42
                    }
                ]
            };

            me.items[3].load(_chartOp4);
        });

        return this;

    }
});